#ifndef WASAPI_LOOPBACK_CAPTURE_SIMPLE_H
#define WASAPI_LOOPBACK_CAPTURE_SIMPLE_H

#include <QObject>
#include <QString>
#include <QFile>
#include <QTimer>
#include "audiotransmitter.h"

// Windows音频相关头文件
#include <windows.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#include <initguid.h>
#include <mmreg.h>

class WasapiLoopbackCaptureSimple : public QObject
{
    Q_OBJECT

public:
    explicit WasapiLoopbackCaptureSimple(QObject *parent = nullptr);
    ~WasapiLoopbackCaptureSimple();

    bool startCapture(const QString &outputFilePath);
    void stopCapture();
    bool isCapturing() const { return m_isCapturing; }

    // 音频传输相关
    bool startTransmission(const QString &host = "localhost", quint16 port = 8766);
    void stopTransmission();
    bool isTransmitting() const { return m_transmitter ? m_transmitter->isTransmissionEnabled() : false; }
    
    // 传输配置
    void setTransmissionEnabled(bool enabled);
    void setTransmissionHost(const QString &host);
    void setTransmissionPort(quint16 port);
    void setTransmissionBufferSize(int size);
    void setTransmissionInterval(int intervalMs);
    void setTransmissionCompression(bool enabled);

signals:
    void captureStatusChanged(bool isCapturing);
    void captureError(const QString &error);
    void captureProgress(qint64 bytesCaptured);
    
    // 传输相关信号
    void transmissionStatusChanged(bool isTransmitting);
    void transmissionError(const QString &error);
    void transmissionDataSent(qint64 bytesSent);
    void transmissionConnectionEstablished();
    void transmissionConnectionLost();

private slots:
    void onCaptureTimer();
    
    // 传输相关槽函数
    void onTransmissionStatusChanged(bool enabled);
    void onTransmissionError(const QString &error);
    void onTransmissionDataSent(qint64 bytesSent);
    void onTransmissionConnectionEstablished();
    void onTransmissionConnectionLost();

private:
    bool initializeWASAPI();
    bool setupLoopbackCapture();
    void writeWavHeader();
    void finalizeWavFile();
    void cleanupWASAPI();
    void captureAudioData();
    
    // WASAPI相关
    IMMDeviceEnumerator *m_deviceEnumerator;
    IMMDevice *m_audioDevice;
    IAudioClient *m_audioClient;
    IAudioCaptureClient *m_captureClient;
    
    // 音频格式
    WAVEFORMATEX *m_waveFormat;
    UINT32 m_bufferFrameCount;
    
    // 捕获相关
    QTimer *m_captureTimer;
    QFile m_outputFile;
    QString m_outputFilePath;
    
    // WAV文件信息
    qint64 m_dataSizePosition;
    qint64 m_fileSizePosition;
    qint64 m_dataStartPosition;
    
    // 状态
    bool m_isCapturing;
    bool m_wasapiInitialized;
    
    // 音频参数
    static const int SAMPLE_RATE = 48000;
    static const int CHANNEL_COUNT = 2;
    static const int SAMPLE_SIZE = 16;
    static const int BUFFER_DURATION = 20; // 20ms buffer，减少延迟

    // 音频传输相关
    AudioTransmitter *m_transmitter;
    bool m_transmissionEnabled;
    QString m_transmissionHost;
    quint16 m_transmissionPort;
};

#endif // WASAPI_LOOPBACK_CAPTURE_SIMPLE_H
